<?php
class returpembeliancontroller extends CI_Controller{
	function __construct(){
		parent::__construct();
		$this->load->helper('form');
		$this->load->library('form_validation');
		$this->load->model('mastersupplier_model');
		$this->load->model('masterbarangdetail_model');
		$this->load->model('stock_model');
		$this->load->model('returpembelian_model');
		$this->load->library('session');
		$this->load->library('pagination_editable');
		if(($this->session->userdata('fullname') == '') || ($this->session->userdata('fullname') == NULL)){
			redirect('logincontroller');
		}
	}
	
	function index()
	{
		$config = array();
		$config['per_page'] = 10;
		$config['first_tag_open'] = '<li>';
		$config['first_tag_close'] = '</li>';
		$config['curr_class'] = 'active';
		$config['full_tag_open'] = '<ul id="pagination-flickr">';
		$config['full_tag_close'] = '</ul>';
		$config['num_tag_open'] = '<li>';
		$config['num_tag_close'] = '</li>';
		$config['next_link'] = 'Next &gt;&gt;';
		$config['prev_link'] = '&lt;&lt; Previous';
		$config['next_tag_open'] = '<li class="next">';
		$config['next_tag_close'] = '</li>';
		$config['prev_tag_open'] = '<li class="previous-off">';
		$config['prev_tag_close'] = '</li>';
		
		$config["uri_segment"] = 3;
		$config["base_url"] = site_url('returpembeliancontroller/index');
		$config['total_rows'] = $this->returpembelian_model->countGetList();
		
		$this->pagination_editable->initialize($config);
	
		$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
		               
		if (isset($_POST['btnview'])) {
            if ((isset($_POST['tglawal']) && isset($_POST['tglakhir'])) && (!empty($_POST['tglawal']) && !empty($_POST['tglakhir'])))
			{
				$this->returpembelian_model->setTanggalAwal($_POST['tglawal']);
				$this->returpembelian_model->setTanggalAkhir($_POST['tglakhir']);
				$dataprofile = array(
					'page_title' => 'Purchasing > returpembelian Barang',
					'name' => $this->session->userdata('fullname'),
					'kodereturno'=> $this->returpembelian_model->getReturnNoCode(),
					'mstrsupp' => $this->mastersupplier_model->getList(),
					'masterbarang' => $this->masterbarangdetail_model->getall(),
					'transretur' => $this->returpembelian_model->getlistpertanggal(),
					'links' => $this->pagination_editable->create_links(),
					'message' =>''
				);  
            }
			elseif($_POST['kdview'] == "0")
			{
				$dataprofile = array(
					'page_title' => 'Purchasing > returpembelian Barang',
					'name' => $this->session->userdata('fullname'),
					'kodereturno' => $this->returpembelian_model->getReturnNoCode(),
					'mstrsupp' => $this->mastersupplier_model->getList(),
					'masterbarang' => $this->masterbarangdetail_model->getall(),
					'transretur' => $this->returpembelian_model->getListlimit($config['per_page'],$page),
					'links' => $this->pagination_editable->create_links(),
					'message' =>''		
				);  
            }
			elseif($_POST['kdview'] == "1")
			{
				$this->returpembelian_model->setReturnNo($_POST['namaview']);
				$dataprofile = array(
					'page_title' => 'Purchasing > returpembelian Barang',
					'name' => $this->session->userdata('fullname'),
					'kodereturno'=> $this->returpembelian_model->getReturnNoCode(),
					'mstrsupp' => $this->mastersupplier_model->getList(),
					'masterbarang' => $this->masterbarangdetail_model->getall(),
					'transretur' => $this->returpembelian_model->getreturnolike(),
					'links' => $this->pagination_editable->create_links(),
					'message' =>''
				);  
            }
			elseif($_POST['kdview'] == "2")
			{
				$this->returpembelian_model->setSuppliername($_POST['namaview']);
				$dataprofile = array(
					'page_title' => 'Purchasing > returpembelian Barang',
					'name' => $this->session->userdata('fullname'),
					'kodereturno'=> $this->returpembelian_model->getReturnNoCode(),
					'mstrsupp' => $this->mastersupplier_model->getList(),
					'masterbarang' => $this->masterbarangdetail_model->getall(),
					'transretur' => $this->returpembelian_model->getkodesupplike(),
					'links' => $this->pagination_editable->create_links(),
					'message' =>''
				);  
            }
			elseif($_POST['kdview'] == "3")
			{
				$this->returpembelian_model->setProductname($_POST['namaview']);
				$dataprofile = array(
					'page_title' => 'Purchasing > returpembelian Barang',
					'name' => $this->session->userdata('fullname'),
					'kodereturno'=> $this->returpembelian_model->getReturnNoCode(),
					'mstrsupp' => $this->mastersupplier_model->getList(),
					'masterbarang' => $this->masterbarangdetail_model->getall(),
					'transretur' => $this->returpembelian_model->getkodebaranglike(),
					'links' => $this->pagination_editable->create_links(),
					'message' =>''
				);  
            }
        } 
		else 
		{
			$dataprofile = array(
				'page_title' => 'Purchasing > returpembelian Barang',
				'name' => $this->session->userdata('fullname'),
				'kodereturno'=> $this->returpembelian_model->getReturnNoCode(),
				'mstrsupp' => $this->mastersupplier_model->getList(),
				'masterbarang' => $this->masterbarangdetail_model->getall(),
				'transretur' => $this->returpembelian_model->getListlimit($config['per_page'],$page),
				'links' => $this->pagination_editable->create_links(),
				'message' =>''		
			);		
		}
		$content = array(
			'content' => 'purchasing/returpembelian/inputreturpembelian'
		);
		$this->template->load('templates/default',$content,$dataprofile);
	}
	
	function tambahreturpembelian() 
	{
		$config = array();
		$config['per_page'] = 10;
		$config['first_tag_open'] = '<li>';
		$config['first_tag_close'] = '</li>';
		$config['curr_class'] = 'active';
		$config['full_tag_open'] = '<ul id="pagination-flickr">';
		$config['full_tag_close'] = '</ul>';
		$config['num_tag_open'] = '<li>';
		$config['num_tag_close'] = '</li>';
		$config['next_link'] = 'Next &gt;&gt;';
		$config['prev_link'] = '&lt;&lt; Previous';
		$config['next_tag_open'] = '<li class="next">';
		$config['next_tag_close'] = '</li>';
		$config['prev_tag_open'] = '<li class="previous-off">';
		$config['prev_tag_close'] = '</li>';
		
		$config["uri_segment"] = 3;
		$config["base_url"] = site_url('returpembeliancontroller/index');
		$config['total_rows'] = $this->returpembelian_model->countGetList();
		
		$this->pagination_editable->initialize($config);
	
		$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
		
		if (isset($_POST['btnSave'])) 
		{	
			if (isset($_POST['keterangan']) && $_POST['keterangan'] !="")
			{				
				$kdnoreturok 	= $this->returpembelian_model->getReturnNoCode();
				$tglretur =date('Y-m-d', strtotime('now'));
				$tanggal=date('Y-m-d H:i:s', strtotime('now'));
				
				$jumdata = count($this->input->post('kdbarang'));
				$i = 0;
				$kodeBrg = "";
				$hrgBrg = "";
				$qtydet = "";
				$disc = "";
				$amtdet = "";
				$ttlqty="";
				$ttlamount="";
				
				foreach($this->input->post('kdbarang') as $kdBarang)
				{
					$kdidok		= $this->returpembelian_model->getdetailid();
					
					$kodeBrg	= $_POST['kdbarang'][$i];
					$hrgBrg		= $_POST['hrgbarang'][$i];
					$qtydet 	= $_POST['qty'][$i];
					$disc 		= $_POST['distcoun'][$i];
					$amtdet 	= $_POST['jmlhhrg'][$i];
					$ttlqty 	= $ttlqty + $qtydet;
					$ttlamount 	= $ttlamount + $amtdet;
					$kdsupp = $_POST['kdsupp'];
					
					$this->returpembelian_model->setReturnNo($kdnoreturok);
					$this->returpembelian_model->setIDdetail($kdidok);
					$this->returpembelian_model->setKodeDetailBarang($kodeBrg);
					$this->returpembelian_model->setHargaBarang(intval($hrgBrg));
					$this->returpembelian_model->setQTY(intval($qtydet));
					$this->returpembelian_model->setDiscount($disc);
					$this->returpembelian_model->setAmount($amtdet);			
					$this->returpembelian_model->insertdetail();
					
					// insert ke tabel stock dan transaksi stock
					$this->stock_model->setKodeSupplier($kdsupp);
					$this->stock_model->setKodeDetailBarang($kodeBrg);
					$stockArray['stok'] = $this->stock_model->cekkodestck();
					$stockkode = $this->stock_model->getStockListId();
					$stockAkhir = $this->stock_model->getStockCurrent();
					$stockstatus = "R";
					
					if($stockArray['stok'] != "0")
					{
						$this->stock_model->setKodeStok($stockkode[0]->kodestok);
						$latest_stock = intval($stockAkhir[0]->stockakhir) - intval($qtydet);
						$this->stock_model->setStokAkhir($latest_stock);
						$this->stock_model->setLastUpdateBy($this->session->userdata('userid'));
						$this->stock_model->setLastUpdateOn($tanggal);
						$this->stock_model->update();
						
						$this->stock_model->setKodeStok($stockkode[0]->kodestok);
						$this->stock_model->setQuantity($qtydet);
						$this->stock_model->setStatus($stockstatus);
						$this->stock_model->setCreatedBy($this->session->userdata('userid'));
						$this->stock_model->setCreatedOn($tanggal);
						$this->stock_model->insertdetail();
					}
					else
					{
						$codestock = $this->stock_model->ambilkodestock();
						$this->stock_model->setKodeStok($codestock);
						$this->stock_model->setKodeSupplier($kdsupp);
						$this->stock_model->setKodeDetailBarang($kodeBrg);
						$this->stock_model->setStokAkhir($qtydet);
						$this->stock_model->setCreatedBy($this->session->userdata('userid'));
						$this->stock_model->setCreatedOn($tanggal);
						$this->stock_model->insert();	
						
						$this->stock_model->setKodeStok($codestock);
						$this->stock_model->setQuantity($qtydet);
						$this->stock_model->setStatus($stockstatus);
						$this->stock_model->setCreatedBy($this->session->userdata('userid'));
						$this->stock_model->setCreatedOn($tanggal);
						$this->stock_model->insertdetail();
					}
					
					//var_dump($kodeBrg." - ".$hrgBrg);
					$i++;
				}
				
				$this->returpembelian_model->setReturnNo($kdnoreturok);
				$this->returpembelian_model->setKodeSupplier($_POST['kdsupp']);
				$this->returpembelian_model->setNoPo("-");
				$this->returpembelian_model->setDateReturn($tglretur);
				$this->returpembelian_model->setQuantity($ttlqty);
				$this->returpembelian_model->setPrice($ttlamount);
				$this->returpembelian_model->setKeterangan($_POST['keterangan']);
				$this->returpembelian_model->setCreatedBy($this->session->userdata('userid'));		
				$this->returpembelian_model->setCreatedOn($tanggal);
						
				$this->returpembelian_model->insert();
				
				
				$dataprofile = array(
					'page_title' => 'Purchasing > returpembelian Barang',
					'name' => $this->session->userdata('fullname'),
					'kodereturno'=> $this->returpembelian_model->ambilnoretur(),
					'transretur' => $this->returpembelian_model->getreturno()	
				);		
				$content = array(
					'content' => 'purchasing/returpembelian/formreturpembelian'
				);
				$this->template->load('templates/default',$content,$dataprofile);
			
			} 
			else 
			{
                $dataprofile = array(
					'page_title' => 'Purchasing > returpembelian Barang',
            		'name' => $this->session->userdata('fullname'),
					'message' => '<div style="color:red;font-size:9pt;font-weight:bold;">Lengkapi Dahulu Form Retur pembelian Barang </div>',
					'kodereturno'=> $this->returpembelian_model->getReturnNoCode(),
					'mstrsupp' => $this->mastersupplier_model->getList(),
					'masterbarang' => $this->masterbarangdetail_model->getall(),
					'transretur' => $this->returpembelian_model->getListlimit($config['per_page'],$page),
					'links' => $this->pagination_editable->create_links()
                );
                $content = array(
                    'content' => 'purchasing/returpembelian/inputreturpembelian'
                );
                $this->template->load('templates/default', $content, $dataprofile);
            }
        }
		else 
		{
			$dataprofile = array(
				'page_title' => 'Purchasing > returpembelian Barang',
				'name' => $this->session->userdata('fullname'),
				'kodereturno'=> $this->returpembelian_model->getReturnNoCode(),
				'mstrsupp' => $this->mastersupplier_model->getList(),
				'masterbarang' => $this->masterbarangdetail_model->getall(),
				'transretur' => $this->returpembelian_model->getListlimit($config['per_page'],$page),
				'links' => $this->pagination_editable->create_links(),
				'message' =>''		
			);		
			$content = array(
				'content' => 'purchasing/returpembelian/inputreturpembelian'
			);
			$this->template->load('templates/default',$content,$dataprofile);
        }
    }
	
	function ubahreturpembelian() 
	{
        $idkdNoRetur = $this->uri->segment(3);
		$idkddetail = $this->uri->segment(4);
		$this->returpembelian_model->setNoPo($idkdNoRetur);
        if (isset($_POST['btnSave'])) 
		{
			$tanggal=date('Y-m-d H:i:s', strtotime('now'));
			
			$this->returpembelian_model->setReturnNo($_POST['kdretur']);
			$this->returpembelian_model->setIDdetail($_POST['kdiddetail']);

			$this->returpembelian_model->setKodeDetailBarang($_POST['kdbarang']);
			$this->returpembelian_model->setHargaBarang($_POST['hrgbarang']);
			$this->returpembelian_model->setQTY($_POST['qty']);
			$this->returpembelian_model->setDiscount($_POST['distcoun']);
			$this->returpembelian_model->setAmount($_POST['jmlhhrg']);

            $this->returpembelian_model->ubahreturpembeliandet();
			
			$ttlqty 	= $this->returpembelian_model->getTotalqty();
			$ttlamount	= $this->returpembelian_model->getTotalamt();
			
			$this->returpembelian_model->setReturnNo($_POST['kdretur']);
			$this->returpembelian_model->setKodeSupplier($_POST['kdsupp']);
			$this->returpembelian_model->setQuantity($ttlqty);
			$this->returpembelian_model->setPrice($ttlamount);
			$this->returpembelian_model->setKeterangan($_POST['keterangan']);
			$this->returpembelian_model->setLastUpdateBy($this->session->userdata('userid'));		
			$this->returpembelian_model->setLastUpdateOn($tanggal);
			
			$this->returpembelian_model->ubahreturpembelian();
			
			if($_POST['kdbarang'] == $_POST['kdbarangawal'])
			{
				$kodebrgstock = $_POST['kdbarang'] ;
			}
			else
			{
				$kodebrgstock = $_POST['kdbarang'] ;
			}
			
			
			// insert ke tabel stock dan transaksi stock
			$this->stock_model->setKodeSupplier($_POST['kdsupp']);
			$this->stock_model->setKodeDetailBarang($kodebrgstock);
			$stockArray = $this->stock_model->getStockListId();
			$stockAkhir = $this->stock_model->getStockCurrent();
			$stockstatus = "R";
			
			if($stockArray != "")
			{
				$this->stock_model->setKodeStok($stockArray[0]->kodestok);
				$latest_stock = intval($stockAkhir[0]->stockakhir) - intval($_POST['qty']) + intval($_POST['jmlhqtyawal']);
				$this->stock_model->setStokAkhir($latest_stock);
				$this->stock_model->setLastUpdateBy($this->session->userdata('userid'));
				$this->stock_model->setLastUpdateOn($tanggal);
				$this->stock_model->update();
				
				$this->stock_model->setKodeStok($stockArray[0]->kodestok);
				$this->stock_model->setQuantity($_POST['qty']);
				$this->stock_model->setStatus($stockstatus);
				$this->stock_model->setCreatedBy($this->session->userdata('userid'));
				$this->stock_model->setCreatedOn($tanggal);
				$this->stock_model->insertdetail();
			}
			else
			{
				$codestock = $this->stock_model->ambilkodestock();
				$this->stock_model->setKodeStok($codestock);
				$this->stock_model->setKodeSupplier($_POST['kdsupp']);
				$this->stock_model->setKodeDetailBarang($kodebrgstock);
				$this->stock_model->setStokAkhir($_POST['qty']);
				$this->stock_model->setCreatedBy($this->session->userdata('userid'));
				$this->stock_model->setCreatedOn($tanggal);
				$this->stock_model->insert();	
				
				$this->stock_model->setKodeStok($codestock);
				$this->stock_model->setQuantity($_POST['qty']);
				$this->stock_model->setStatus($stockstatus);
				$this->stock_model->setCreatedBy($this->session->userdata('userid'));
				$this->stock_model->setCreatedOn($tanggal);
				$this->stock_model->insertdetail();
			}

            redirect('returpembeliancontroller');
        } 
		else 
		{
            $this->returpembelian_model->setReturnNo($idkdNoRetur);
			$this->returpembelian_model->setIDdetail($idkddetail);
            $dataprofile = array(
				'page_title' => 'Purchasing > returpembelian Barang',
				'name' => $this->session->userdata('fullname'),
				'mstrsupp' => $this->mastersupplier_model->getList(),
				'masterbarang' => $this->masterbarangdetail_model->getall(),
				'transretur' => $this->returpembelian_model->getkodereturno(),
				'links' => $this->pagination_editable->create_links()		
			);		
			$content = array(
				'content' => 'purchasing/returpembelian/editreturpembelian'
			);
			$this->template->load('templates/default',$content,$dataprofile);
        }
    }

    function deletereturpembelian() 
	{
        $Kodereturno = $this->uri->segment(3);
        $this->returpembelian_model->setReturnNo($Kodereturno);
        $this->returpembelian_model->deletereturpembelian();
        redirect('returpembeliancontroller');
    }
	
	function cetak_faktur()
	{
		$idkdNoretur 	= $this->uri->segment(3);
		$idkddetailbrg 	= $this->uri->segment(4);
		
		$this->returpembelian_model->setReturnNo($idkdNoretur);
		$this->returpembelian_model->setIDdetail($idkddetailbrg);
		
		$dataprofile = array(
			'page_title' => 'Purchasing > Penerimaan Barang',
			'name' => $this->session->userdata('fullname'),
			'kodereturno'=> $this->returpembelian_model->ambilnoreturkwitansi(),
			'transretur' => $this->returpembelian_model->getListreturkwitansi()	
		);		
		$content = array(
			'content' => 'purchasing/returpembelian/formreturpembelian'
		);
		$this->template->load('templates/default',$content,$dataprofile);
		
	}
}